<script type="text/javascript">
$(function(){
	var pg = App.message.topicEdit.ui, pp = pg.params || {},
		title = pg.children('.navi').seek('title'),
		gd = pg.seek('grid'), recipients=[];

	title.text("添加主题");
	var frm = {
		code: pg.seek('code'),
		name: pg.seek('name'),
		priority: pg.seek('priority'),
		capacity: pg.seek('capacity'),
		term: pg.seek('term'),
		reliability: pg.seek('reliability'),
		catalog: pg.seek('catalog')
	};
	
	frm.reliability.mac('combo', {
		keyField: 'k',
		displayField: 'v',
		multiSelect: false,
		width: 340,
		boxWidth: 341,
		cols : [{
			field: 'v', width: 340
		}],
		data: [{ k: 0, v: '内存主题' },{ k: 10, v: '同步持久化主题' }, { k: 11, v: '同步持久化主题 +内存副本' }]
	});

	function getValues(){
		return {
			id: pg.params.id,
			code: frm.code.val(),
			name: frm.name.val(),
			priority: frm.priority.val(),
			capacity: frm.capacity.val(),
			term: frm.term.val(),
			reliability: frm.reliability.selected[0],
			catalog:frm.catalog.val(),
			type:1,//主题
			recipients:recipients.join(';')
		};
	}
	
	function setRecipients(rec) {
		if(rec!=''){
			recipients=rec.split(";");
		}
		updateSub(recipients);
	}
	//设置表单值
	function setValues(o){
		pg.params.id=o.id;
		frm.code.val(o.code);
		frm.name.val(o.name);
		frm.priority.val(o.priority);
		frm.capacity.val(o.capacity);
		frm.term.val(o.term);
		frm.reliability.val(o.reliability);
		frm.catalog.val(o.catalog);
	}
	
	//验证方法
	function validate1(p){
		var x2 = App.validate2, v = App.base.validation, m = pg.find('.msg').html('');
		var vd = true
			&& x2(v.admin.name, frm.catalog)
			&& x2(v.svc.version, frm.code)
			&& x2(v.svc.name, frm.name);
		return vd;
	}
	
	//失去焦点判断
	pg.find('input').blur(function(e){
		var pms = getValues();
		if(!validate1(pms,e.currentTarget.name))
			return;
	});

	
	pg.seek('back').click(function(){
		App.show({
			ctx: 'message',
			action: 'queueIndex',
			params: {}
		}, $.noop, pg.parent());
	});
	//订阅人增删
	function updateSub(arr){
		var addForm=pg.seek('addForm');
		addForm.empty();
		for(var index in arr){
			addForm.append('<div class="row" >'+
					'<div class="col-90">'+arr[index]+'</div>'+
					'<div class="col-10 fr"><a class="delete" '+'name=deleteSub_'+index+' >删除</a></div>'+
					'</div>');
		}
		if(arr.length>0){
			$('.delete').click(function(e){
				var deleteIndx=e.currentTarget.name.substr(10);
				mac.confirm('确定要删除订阅人:'+recipients[deleteIndx]+'?', function(){
					var tempArr=[],index2=0;
					for(var index1 in recipients){
						if(index1!=deleteIndx){
							tempArr[index2]=recipients[index1];
							index2++;
						}
					}
					recipients=tempArr;
					updateSub(recipients);
				});
			})
		}
	}

	pg.seek('ok').click(function(){
		var pms = getValues();
		if(!validate1(pms))//触发验证
			return;
		$.post(App.ctx + '/target/queue/save.do', pms, function(rs){
			var ro = mac.eval(rs);
			if (ro.success) {
				App.show({
					ctx : 'message',
					action : 'queueIndex',
					params : {}
				}, $.noop, pg.parent());
			} else {
				pg.seek('msg').html(mac.msg(ro.data, ro.params));
			}
		});
		return false;
	});

	if (pp.id) {
		title.text("修改主题");//修改
		$.get(App.ctx + '/target/queue/get.do', {
			id : pg.params.id
		}, function(rs) {
			var ro = mac.eval(rs);
			if (ro.success) {
				setValues(ro.data);
				setRecipients(ro.data.recipients);
			} else {
				pg.seek('msg').html(mac.msg(ro.data, ro.params));
			}
		}); 
	}
});
</script>
<div class="navi">
	<span class="a" name="back">队列与主题</span>&gt;&gt;
	<span name="title">XX主题</span>
</div>
<div class="form">
	<div class="item">
		<span class="label">所属系统：</span>
		<input class="input" type="text" name="catalog" placeholder="请填入所属系统" />
		<span class="msg">*</span>
		<div class="clear"></div>
	</div>
	<div class="item">
		<span class="label">主题编号：</span>
		<input class="input" type="text" name="code placeholder="请填入主题编号" />
		<span class="msg">*</span>
		<div class="clear"></div>
	</div>
	<div class="item">
		<span class="label">主题名称：</span>
		<input class="input" type="text" name="name" placeholder="请填入主题名称" />
		<div class="clear"></div>
	</div>
	<div class="item">
		<span class="label">优先级：</span>
		<input type="number" name="priority" value="5" /> 
		<div class="clear"></div>
	</div>
	<div class="item">
		<span class="label">最大长度：</span>
		<input type="number" name="capacity" value="30000" />
		<div class="clear"></div>
	</div>
	<div class="item">
		<span class="label">消息有效期：</span>
		<input type="number" name="term" value="60" />
		<div class="clear"></div>
	</div>
	<div class="item">
		<span class="label">可靠性：</span>
		<div class="combo fl" name="reliability" ></div>
		<div class="clear"></div>
	</div>
	<div class="item">
		<span class="label">订阅人：</span>
		<textarea name="subscriber"></textarea>
		<div class="clear"></div>
	</div>
	<div class="item">
		<span class="label"></span>
		<span class="msg" name="msg"></span>
	</div>
	<div class="bottom">
		<button name="ok">提交</button>
	</div>
</div>